import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
  ...HttpUtils.func,
  data: {
    titOn: 'titOn1', //资源排行默认图标
    newsHide: 'newsShow',
    videoHide: 'videoHide',
    contentType: 'Pic',
    studyPageConst: {
      //图文栏目ID
      tuwenColumnId: 8023,
      //视频栏目ID
      tvColumnId: 8050,
      //学习专题栏目ID
      topicColumnId: 8075,
      // //党建电视栏目ID
      // djtvColumnId: 8130,
      // //远程教院栏目ID
      // yjyyColumnId: 2851,
      // //课程中心栏目ID
      // kczxColumnId: 8050,
    },
    tuwenColumnList: [],
    tvColumnList: [],
    topicList: [],
    resourceList: [],
    djtvColumnList: [],
    yjyyColumnList: [],
    kczxColumnList: [],
    flag: true,
  },
  onLoad() {
    console.log("进入学习资源菜单");
    //加载图文栏目
    this.loadtuwenColumnId(this.data.studyPageConst.tuwenColumnId);
    //加载专题内容
    this.loadContentList();
  },

  //图文视频公用方法
  tuwenTvUtils: function(content) {
    var contentList = [];
    var json = {};
    json["code"] = content.code;
    json["description"] = content.description;
    json["iconUrl"] = content.iconUrl;
    json["leaf"] = content.leaf;
    json["name"] = content.name;
    json["parentFolderCode"] = content.parentFolderCode;
    json["siteCode"] = content.siteCode;
    contentList.push(json);
    return contentList;
  },

  //加载图文栏目
  loadtuwenColumnId: function(parentId) {
    var newUrl = getApp().globalData.dangjianUrl + "/content/folderQuery";
    var params = {};
    params["code"] = parentId + '';
    params["siteCode"] = getApp().globalData.siteCode + "";
    params = JSON.stringify(params);

    this.httpGet(newUrl, params, "栏目查询失败").then(
      (respData) => {
        var result = respData.data.result.code;
        if (1 == result) {
          var content = respData.data.folder;
          var contentList = this.tuwenTvUtils(content);
          this.setData({
            "tuwenColumnList": contentList
          });
        } else {
          this.showPageMsg("alert", respData.data.result.description)
        }
        this.loadTvColumnId(this.data.studyPageConst.tvColumnId);
      }
    );
  },

  //加载视频栏目
  loadTvColumnId: function(parentId) {
    var newUrl = getApp().globalData.dangjianUrl + "/content/folderQuery";
    var params = {};
    params["code"] = parentId + '';
    params["siteCode"] = getApp().globalData.siteCode + "";
    params = JSON.stringify(params);
    this.httpGet(newUrl, params, "栏目查询失败").then(
      (respData) => {
        var result = respData.data.result.code;
        if (1 == result) {
          var content = respData.data.folder;
          var contentList = this.tuwenTvUtils(content);
          this.setData({
            "tvColumnList": contentList
          });
        } else {
          this.showPageMsg("alert", respData.data.result.description)
        }

      }
    );
  },

  //加载专题内容
  loadContentList: function(e) {
    //加载栏目下内容
    var folderCode = this.data.studyPageConst.topicColumnId;
    var newUrl = getApp().globalData.dangjianUrl + "/content/relativeContentQuery";
    var params = {};
    params["pageIndex"] = "1";
    params["pageItems"] = "2";
    params["code"] = folderCode + "";
    params["siteCode"] = getApp().globalData.siteCode + "";
    params = JSON.stringify(params);
    this.httpGet(newUrl, params, "学习专题下内容加载失败").then(
      (respData) => {
        var result = respData.data.result.code;
        if (1 == result) {
          var contents = respData.data.contents.content;
          var contentList = [];
          for (var index in contents) {
            var content = contents[index];
            var json = {};
            var thumbnail = content.thumbnail;
            if (null == thumbnail || "" == thumbnail) {
              var imageFiles = content.imageFiles;
              for (var j in imageFiles) {
                thumbnail = imageFiles[j].url;
                break;
              }
            }
            json["name"] = content.name;
            json["publishTime"] = content.publishTime;
            json["url"] = thumbnail;
            json["code"] = content.code;
            json["type"] = content.type;
            json["foldCode"] = folderCode;
            json["count"] = content.count;
            json["fromNa"] = '' == content.fromNa ? "华数" : content.fromNa;
            json["folderCode"] = folderCode;
            contentList.push(json);
          }
          this.setData({
            "topicList": contentList
          });
        } else {
          this.showPageMsg("alert", respData.data.result.description)
        }
        // 加载资源排行栏目
        this.loadResourceRankList();
      }
    );
  },


  handleTap1(event) { //图文 视频切换
    var titOn = event.target.id;
    //请求
    this.setData({
      contentType: event.currentTarget.dataset.contentType,
      titOn: titOn,
    })
    var flag;
    var newsHide;
    var videoHide;
    var titOn = this.data.titOn;
    //渲染
    if (titOn == 'titOn1') { //图文
      newsHide = "newsShow";
      videoHide = 'videoHide';
      flag = true;
    } else { //视频
      newsHide = "newsHide";
      videoHide = 'videoShow';
      flag = false;
    }
    this.setData({
      newsHide: newsHide,
      videoHide: videoHide,
      flag: flag,
    })
    this.loadResourceRankList();
  },


  // 加载图文资源排行栏目
  loadResourceRankList: function() {
    var showNoData = true;
    var newUrl = getApp().globalData.dangjianUrl + "/content/contentTop";
    var params = {};
    params["pageIndex"] = "1";
    params["pageItems"] = "10";
    params["siteCode"] = getApp().globalData.siteCode + "";
    params["contentType"] = "Pic";
    params = JSON.stringify(params);
    this.httpGet(newUrl, params, "加载图文资源排行栏目加载失败").then(
      (respData) => {
        var result = respData.data.result.code;
        if (1 == result) {
          var contents = respData.data.contents.content;
          for (var index in contents) {
            var content = contents[index];
            var thumbnail = content.thumbnail;
            if (null == thumbnail || "" == thumbnail) {
              var imageFiles = content.imageFiles;
              for (var j in imageFiles) {
                thumbnail = imageFiles[j].url;
                break;
              }
            }
            content.thumbnail = thumbnail;
            content.fromNa = '' == content.fromNa ? "华数" : content.fromNa;
          }
          console.log(content)
          if (content != undefined) {
            showNoData = false;
          } else {
            showNoData = true;
          }

          this.setData({
            "resourceList": contents,
            showNoData: showNoData,
          });

        } else {
          this.showPageMsg("alert", respData.data.result.description)
        }
        this.loadVideoList();
      }
    );
  },

  // 加载视频资源排行栏目
  loadVideoList: function() {
    var newUrl = getApp().globalData.dangjianUrl + "/content/contentTop";
    var params = {};
    params["pageIndex"] = "1";
    params["pageItems"] = "10";
    params["siteCode"] = getApp().globalData.siteCode + "";
    params["contentType"] = "Video";
    params = JSON.stringify(params);
    this.httpGet(newUrl, params, "加载视频资源排行栏目加载失败").then(
      (respData) => {
        var result = respData.data.result.code;
        if (1 == result) {
          var contents = respData.data.contents.content;
          for (var index in contents) {
            var content = contents[index];
            var thumbnail = content.thumbnail;
            if (null == thumbnail || "" == thumbnail) {
              var imageFiles = content.imageFiles;
              for (var j in imageFiles) {
                thumbnail = imageFiles[j].url;
                break;
              }
            }
            content.thumbnail = thumbnail;
            content.fromNa = '' == content.fromNa ? "华数" : content.fromNa;
          }
          console.log(content)
          this.setData({
            "videoList": contents
          });

        } else {
          this.showPageMsg("alert", respData.data.result.description)
        }
      }
    );
  },

  goTuWenPage(e) {
    wx.navigateTo({
      url: "/pages/news/news?folderCode=8023&currentFolderCode=8000",
    })
  },

  goTvPage(e) {
    var folderCode = e.currentTarget.dataset.code;
    wx.navigateTo({
      url: '../video/video?folderCode=' + folderCode,
    })
  },

  //栏目内容详情展示
  articleDetail(e) {
    var folderCode = e.currentTarget.dataset.foldercode;
    var type = e.currentTarget.dataset.type;
    var code = e.currentTarget.dataset.code;
    var name = e.currentTarget.dataset.name;
    var detailUrl = this.getUrl(type, code, folderCode, name);
    if (detailUrl.length > 0) {
      wx.navigateTo({
        url: detailUrl
      });
    } else {
      this.showPageMsg("tips", "该内容类型详情页暂未开发") 
    }
  },
  // 页面被拉到底部  逻辑:要求资源排行前10条
  onReachBottom() {
    this.showPageMsg("tips", "没有更多数据了!");
    // if (this.data.pageIndex > this.data.totalPages) {
    // 	this.showPageMsg("tips", "没有更多数据了!");
    // } else {
    // 	if (thsi.data.contentType == 'Pic') {
    // 		this.loadResourceRankList();
    // 	} else {
    // 		this.loadVideoList();
    // 	}
    // }
  },

  // 页面 停止上拉刷新
  onPullDownRefresh: function(e) {
    wx.stopPullDownRefresh();
  },

  shortVideo(e) { //党建电视
    var folderCode = e.currentTarget.dataset.code;
    wx.navigateTo({
      url: '../shortVideo/shortVideo?folderCode=' + folderCode
    })
  }
});